package com.itmg.search.business.bean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.itmg.cache.CacheContext;
import com.itmg.search.form.SearchForm;
import com.itmg.search.web.DataItemView;
import com.itmg.tools.bean.PriceBean;
import com.itmg.util.Constants;
import com.itmg.vo.SearchResultItemVO;

public class SearchBean {

	public SearchBean() {
	}
	
	public static String getCondition_text(SearchForm searchForm){
		String condition_text = "";
		if(searchForm.getKeywords() != null && !"".equals(searchForm.getKeywords()))
			condition_text += searchForm.getKeywords();
		if(searchForm.getCategory() != null && !"".equals(searchForm.getCategory()))
			condition_text += " " + searchForm.getCategory();
		if(searchForm.getMinPrice() != null && searchForm.getMaxPrice() != null)
			if(searchForm.getMaxPrice() == 0)
				condition_text += " " + searchForm.getMinPrice()+"以上";
			else
				condition_text += " " + searchForm.getMinPrice()+"-"+searchForm.getMaxPrice();
		return condition_text;
	}
	
	/**
	 * 将List<SearchResultItemVO>转化成List<DataItemView>
	 * @param List<SearchResultItemVO>
	 * @return
	 */
	public static List<DataItemView> getDataItemViewList(List<SearchResultItemVO> resultItemList)
	{
		List<DataItemView> dataItemViewList = new ArrayList<DataItemView>();
		for(SearchResultItemVO searchResultItemVO : resultItemList)
		{
			DataItemView dataItemView = new DataItemView();
			dataItemView.setProductID(searchResultItemVO.getProductID());
			String productName = searchResultItemVO.getProductName();
			if(productName != null && productName.length() > 35)
			{
				dataItemView.setProductName(productName.substring(0, 23)+" ...");
			}
			else
			{
				dataItemView.setProductName(productName);
			}
			dataItemView.setProductUrl(searchResultItemVO.getProductUrl());
			String comment = searchResultItemVO.getComment();
			if(searchResultItemVO.getMakerName() != null)
				comment += searchResultItemVO.getMakerName(); 
			if(comment != null){
				if(comment.length()>80)
					dataItemView.setComment(comment.substring(0,70)+" ...");
				else
					dataItemView.setComment(comment);
			}else{
				dataItemView.setComment(searchResultItemVO.getProductName());
			}
//			if(comment != null && comment.length()>=50)
//			{
//				dataItemView.setComment(comment);
//			}
//			else if(comment != null && comment.length()<50)
//			{
//				dataItemView.setComment(productName +"  "+ comment);
//				if(dataItemView.getComment().length() < 50){
//					dataItemView.setComment(dataItemView.getComment()+"  "+searchResultItemVO.getMakerName());
//				}
//			}else{
//				if(comment == null)
//					dataItemView.setComment(productName);
//				else
//					dataItemView.setComment(comment);
//			}
			//是否进行价格比较判断
			if(searchResultItemVO.getProductName() != null && !"".equals(searchResultItemVO.getProductName()))
			{
				String keyword = SearchBean.getCompareTrem(searchResultItemVO.getProductName());
				if(keyword != null && !"".equals(keyword)){
					dataItemView.setIsCompare("1");
				}
			}
			if(searchResultItemVO.getMaxPrice() != 0)
			{
				Integer privce = Integer.valueOf(searchResultItemVO.getMaxPrice());
				java.text.DecimalFormat format = new java.text.DecimalFormat("#,##0");
		    	String privce_view = format.format(privce)+"円";
		    	dataItemView.setPrice(privce_view);
			}
			if(searchResultItemVO.getLowestPrice() != 0)
			{
				Integer privce_min = Integer.valueOf(searchResultItemVO.getLowestPrice());
				java.text.DecimalFormat format_min = new java.text.DecimalFormat("#,##0");
		    	String privce_view = format_min.format(privce_min);
		    	if(searchResultItemVO.getMaxPrice() != 0)
		    	{
		    		Integer privce_max = Integer.valueOf(searchResultItemVO.getMaxPrice());
		    		java.text.DecimalFormat format_max = new java.text.DecimalFormat("#,##0");
		    		privce_view = privce_view +"円 ～"+format_max.format(privce_max)+"円 ";
		    	}
		    	else
		    	{
		    		privce_view = privce_view + "円 ";
		    	}
		    	dataItemView.setPrice(privce_view);
			}
			if(searchResultItemVO.getNowPrice()!= 0)
			{
				Integer privce_min = Integer.valueOf(searchResultItemVO.getNowPrice());
				java.text.DecimalFormat format_min = new java.text.DecimalFormat("#,##0");
		    	String privce_view = format_min.format(privce_min);
		    	String priceCom = "即決価格："+privce_view+",  入札件数："+searchResultItemVO.getCount()+",  終了日時："+searchResultItemVO.getSaleEndTime()+"。<br/>";
		    	dataItemView.setComment(priceCom+dataItemView.getComment());
			}
			//人民币价格专用
			if(searchResultItemVO.getRmbPrice() != 0.0){
				String privce_view = String.valueOf(searchResultItemVO.getRmbPrice());
				privce_view += " RMB";
				dataItemView.setRmbPrice(privce_view);
				dataItemView.setIsPrice(1);
			}
			//美元价格专用
			if(searchResultItemVO.getMyPrice() != 0.0){
				String privce_view = String.valueOf(searchResultItemVO.getMyPrice());
				privce_view += " USD";
				dataItemView.setMyPrice(privce_view);
				dataItemView.setIsPrice(2);
			}
			dataItemView.setMakerName(searchResultItemVO.getMakerName());
			if(searchResultItemVO.getSmallImageUrl() != null && !"".equals(searchResultItemVO.getSmallImageUrl()))
			{
				String imageURL = searchResultItemVO.getSmallImageUrl();
				dataItemView.setSmallImageUrl(imageURL);
			}
			dataItemView.setBigImageUrl(searchResultItemVO.getBigImageUrl());
			dataItemView.setShopName(searchResultItemVO.getShopName());
			dataItemView.setShopUrl(searchResultItemVO.getShopUrl());
			dataItemView.setProductMd5(searchResultItemVO.getProductMd5());
			dataItemView.setSourceSiteName(searchResultItemVO.getSourceSiteName());
			if(searchResultItemVO.getEvaluation() != null)
				dataItemView.setEvaluation(searchResultItemVO.getEvaluation());
			else
				dataItemView.setEvaluation(0.0);
			dataItemViewList.add(dataItemView);
		}
		return dataItemViewList;
	}
	
	//从商品名字中找除英文和数字
	public static String getCompareTrem(String productName){
		Set<String> compareTremSet = new HashSet<String>();
		
		Pattern pattern = Pattern.compile(Constants.REGEX_ITEM_NAME);
		Matcher matcher = pattern.matcher(productName);
		while(matcher.find()){
			String number = matcher.group();
			if(number != null && !"".equals(number)){
				compareTremSet.add(number);
			}
		}
		String keyword = "";
		for(String compareTrem : compareTremSet){
			if(compareTrem.length()>1){
				keyword += compareTrem+" ";
			}
		}
		return keyword;
	}
	//价格区间处理
	public static List<PriceBean> priceProcessing(List<SearchResultItemVO> resultItemList){
		List<PriceBean> priceList = new ArrayList<PriceBean>();	
		if(resultItemList == null || resultItemList.size()==0)
			return priceList;
		List<SearchResultItemVO> voList = new ArrayList<SearchResultItemVO>();
		voList.addAll(resultItemList);
		CacheContext.sort(Constants.SORT_PRICE_UP_TO_DOWN, voList);//从高到低进行排序
		Integer maxPrice = null;
		Integer minPrice = null;
		if(voList != null){
			maxPrice = voList.get(0).getLowestPrice(); 
			minPrice = voList.get((voList.size())-1).getLowestPrice();
		}
		String str_max = String.valueOf(maxPrice);
		String str_end = "";
		if(str_max.length()>4)
			str_end = str_max.substring(str_max.length()-4,str_max.length());
		else if(str_max.length()>3)
			str_end = str_max.substring(str_max.length()-3,str_max.length());
		else if(str_max.length()>2)
			str_end = str_max.substring(str_max.length()-2,str_max.length());
		else{
			PriceBean bean = new PriceBean();
			bean.setMinPrice(1);
	        bean.setMaxPrice(maxPrice);
	        priceList.add(bean);
	        return priceList;
		}
		Integer max = maxPrice - Integer.parseInt(str_end);
		Integer zon = max/5;
		for(int i=0,j=0;i<maxPrice;j++){
			PriceBean bean = new PriceBean();
			if(j<5){
				if(j==0 && minPrice<(i+zon)){
					if(minPrice<=(i+zon)){
						bean.setMinPrice(i+1);
				        bean.setMaxPrice(i+zon);
				        priceList.add(bean);
					}
				}else{
					if(minPrice<(i+zon)){
						bean.setMinPrice(i+1);
				        bean.setMaxPrice(i+zon);
				        priceList.add(bean);
					}
				}
			}else{
				bean.setMinPrice(i+1);
				priceList.add(bean);
			}
			i = i + zon;
			if(j==5)
				break;
		}
		return priceList;
	}
	
}
